package org.example.jedis;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 宋孟林
 * @description 五大数据类型家庭作业调用api
 * @create: 2025/3/7 17:00
 */
public class JedisHw {
    private static final String MESSAGE = "OK";
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.100.133", 6379);
        jedis.auth("123456");
        //五大数据类型
        //string
        jedis.set("k4", "k4_redis");
        System.out.println("============1===============");
        jedis.mset("k1", "v1", "k2", "v2", "k3", "v3");
        System.out.println(jedis.mget("k1", "k2", "k3"));
        System.out.println("===========2====================");
        //list
        jedis.rpush("l:num", "1", "2", "3", "4", "5");
        List<String> list = jedis.lrange("l:num", 0, -1);
        list.forEach(System.out::print);
        System.out.println();
        System.out.println("===========3====================");
        //set
        jedis.sadd("orders", "jd001");
        jedis.sadd("orders", "jd002");
        jedis.sadd("orders", "jd003");
        jedis.sadd("orders", "jd001");
        Set<String> orders = jedis.smembers("orders");
        orders.forEach(System.out::println);
        long l = jedis.srem("orders", "jd001");
        if (l > 0){
            System.out.println("删除了" + l + "个jd001");
        }else {
            System.out.println("删除失败");
        }
        System.out.println("==========4=====================");
        //z:set
        jedis.zadd("z:set01", 10d, "python");
        jedis.zadd("z:set01", 15d, "java");
        jedis.zadd("z:set01", 20d, "c++");
        jedis.zadd("z:set01", 25d, "javascript");
        jedis.zadd("z:set01", 30d, "shell");
        jedis.zadd("z:set01", 35d, "mysql");
        List<String> stringList = jedis.zrange("z:set01", 0, -1);
        stringList.forEach(System.out::println);
        System.out.println("=============5==================");
        //hash
        jedis.hset("h:user:001", "name", "tiantian");
        jedis.hset("h:user:001", "phone", "12345");
        jedis.hset("h:user:001", "home", "jiangxi");
        List<String> stringList1 = jedis.hmget("h:user:001", "name", "phone", "home");
        stringList1.forEach(System.out::println);
        Map<String, String> map = new HashMap<>(10);
        map.put("name","xiaozhang");
        map.put("phone","98765");
        map.put("address","beijing");
        String result = jedis.hmset("h:user:002", map);
        if (MESSAGE.equals(result)){
            System.out.println("map插入成功");
        }else {
            System.out.println("插入失败");
        }
        System.out.println("===============6================");
        jedis.close();
    }
}
